Query2XML Component
What Is the Query2XML Component?
The Query2XML component transforms a query object into an XML document. The advantage of using Query2XML over WDDX is that the structure of the XML that Query2XML creates is much more human-readable than what WDDX generates, which means it is ideal for storing application configuration information and other data that needs to be updated later by hand.
For instance, you might maintain all your application's configuration information in an XML file that you want to be able to edit yourself using a text editor, or programmatically through a user interface. You can use the XML2Query component to turn the XML into a query, make changes, or create a new query object, and then use Query2XML to turn it back into XML for saving.
Installing the Query2XML Component
The Query2XML component is contained in the com.macromedia.xml package. The easiest way to install it is to create the directory structure com/macromedia/xml in your web root. For instance, if your web root is wwwroot, then query2xml.cfc should be here:
wwwroot/com/macromedia/xml/query2xml.cfc
You can also place the component in a directory under a ColdFusion mapping or in a custom tag root.
Using the Query2XML Component
To transform a query object into XML using the Query2XML component, create an instance of the component using either the cfobject tag or the createObject function, set a query object on the instance using the setQuery function, and call either getXMLObject or getXMLString.
The code below illustrates the simplest usage:
<cfobject component="com.macromedia.xml.query2xml" name="query2xml" /> <cfinvoke component="#query2xml#" method="setQuery" queryObj="#myQuery#" /> <cfinvoke component="#query2xml#" method="getXmlString" returnVariable="myXml" />
By default, the root node of the document is called "root" and the tag that gets repeated for each row in the query is called "row." Inside each row tag is a tag for each column in the query containing cell-specific character data from the query.
You can change the name of the root node using the setRootNode function on your Query2XML component instance before calling getXmlString or getXmlObject. You can also change the name of the tag that's repeated for every row to anything you want using the setRowName function. Finally, you can set the encoding type of the generated XML file using the setXmlEncoding function.
Below is a more comprehensive usage of the Query2XML component:
<cfobject component="com.macromedia.xml.query2xml" name="query2xml" /> <cfinvoke component="#query2xml#" method="setQuery" queryObj="#myQuery#" /> <cfinvoke component="#query2xml#" method="setRootNode" rootNode="rss" /> <cfinvoke component="#query2xml#" method="setRowName" rowName="items" /> <cfinvoke component="#query2xml#" method="setXmlEncoding" encoding="UTF-8" /> <cfinvoke component="#query2xml#" method="getXmlString" returnVariable="myXml" />
The Query2XML component uses the XML Indenter component (included in DRK Volume 4) to indent the generated XML properly, so be sure to install the XML Indenter component before using Query2XML. See the XML Indenter documentation for instructions.
How the Query2XML Component Works
The Query2XML component iterates through the specified query using the query's column names as tag names in the generated XML document. Each row of data is wrapped in a row tag.
For instance, if you had a query containing two rows and the columns firstName, lastName, and email address, your generated XML would look like this:
<?xml version="1.0"?> <root> <row> <firstName>Christian</firstName> <lastName>Cantrell</lastName> <emailAddress>cantrell@macromedia.com</emailAddress> </row> <row> <firstName>Mike</firstName> <lastName>Chambers</lastName> <emailAddress>mesh@macromedia.com</emailAddress> </row> </root>
As previously mentioned, you can change the name of the XML document's root node using the setRootNode function and you can change the name of the row tags using the setRowName function.
The Query2XML component provides a quick and simple mechanism for serializing your query objects in a way that anyone can read and maintain, as well converting them back into a query using Query2XML's counterpart, XML2Query, also included in DRK Volume 4.